package zzh.com.myapplication.test;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import zzh.com.myapplication.R;
import zzh.com.myapplication.test.view.OrderFragment;

/**
 * @author zzh
 * @Name:
 * @Description:
 * @date
 * @time
 * @copyright
 */
public class TabActivity extends AppCompatActivity {
    @Bind(R.id.viewpager)
    ViewPager mVPager;
    @Bind(R.id.tablayout)
    TabLayout mTabLayout;
    public NavigationAdapter adapter;
    public View view;
    public TextView text;
    public List<String> title = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);
        ButterKnife.bind(this);
        title.add("0");
        title.add("1");
        title.add("2");
        title.add("3");
        initDate();
    }

    private static class NavigationAdapter extends CacheFragmentStatePagerAdapter {
        List<String> type;

        public NavigationAdapter(FragmentManager fm, List<String> type) {
            super(fm);
            this.type = type;
        }

        @Override
        protected OrderFragment createItem(int position) {
            return OrderFragment.newInstance(position);
        }

        @Override
        public int getCount() {
            return type.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return type.get(position);
        }


    }

    private void initDate() {
        adapter = new NavigationAdapter(getSupportFragmentManager(), title);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setTabTextColors(getResources().getColor(R.color.white),  //mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
                getResources().getColor(R.color.cardview_dark_background));
        mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.white));
        mTabLayout.setSelectedTabIndicatorHeight(0);
        mVPager.setOffscreenPageLimit(4);
        mVPager.setAdapter(adapter); //ViewCompat.setElevation(mTabLayout, getResources().getDimension(R.dimen.appbar_elevation));
        mTabLayout.setupWithViewPager(mVPager);
    }


    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    // 当屏幕旋转或者配置改变的时候，我们需要保存当前的状态。
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
